package com.deercrowing.takeaway.controller.admin;

import com.deercrowing.takeaway.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Validated
@RestController
@RequestMapping("admin/shop")
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置店铺的营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    public ResponseEntity setStatus(@PathVariable String status){
        log.info("设置店铺的营业状态为：{}","1".equals(status) ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(KEY,status);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 获取店铺的营业状态
     * @return
     */
    @GetMapping("/status")
    public ResponseEntity getStatus(){
        String status = (String) redisTemplate.opsForValue().get(KEY);
        log.info("获取到店铺的营业状态为：{}","1".equals(status) ? "营业中" : "打烊中");
        return ResponseEntity.ok(Result.OK(status));
    }
}
